{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.environ['CUDA_VISIBLE_DEVICES'] = ''\n",
    "os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import json\n",
    "import tensorflow as tf\n",
    "import itertools\n",
    "import collections\n",
    "import re\n",
    "import random\n",
    "import sentencepiece as spm\n",
    "from tqdm import tqdm\n",
    "import xlnet_utils as squad_utils"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from prepro_utils import preprocess_text, encode_ids\n",
    "\n",
    "sp_model = spm.SentencePieceProcessor()\n",
    "sp_model.Load('sp10m.cased.v9.model')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import logging\n",
    "\n",
    "tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)\n",
    "tf.get_logger().setLevel(logging.ERROR)\n",
    "tf.autograph.set_verbosity(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "130318"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_file = '/home/husein/pure-text/ms-train-2.0.json'\n",
    "train_examples = squad_utils.read_squad_examples(\n",
    "      input_file=train_file, is_training=True)\n",
    "\n",
    "len(train_examples)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "11858"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_file = '/home/husein/pure-text/ms-dev-2.0.json'\n",
    "test_examples = squad_utils.read_squad_examples(\n",
    "      input_file=test_file, is_training=False)\n",
    "\n",
    "len(test_examples)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 130318/130318 [4:08:23<00:00,  8.74it/s]   \n"
     ]
    }
   ],
   "source": [
    "max_seq_length = 512\n",
    "doc_stride = 128\n",
    "max_query_length = 64\n",
    "\n",
    "train_features = squad_utils.convert_examples_to_features(\n",
    "      examples=train_examples,\n",
    "      sp_model=sp_model,\n",
    "      max_seq_length=max_seq_length,\n",
    "      doc_stride=doc_stride,\n",
    "      max_query_length=max_query_length,\n",
    "      is_training=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 11858/11858 [22:43<00:00,  8.69it/s] \n"
     ]
    }
   ],
   "source": [
    "test_features = squad_utils.convert_examples_to_features(\n",
    "      examples=test_examples,\n",
    "      sp_model=sp_model,\n",
    "      max_seq_length=max_seq_length,\n",
    "      doc_stride=doc_stride,\n",
    "      max_query_length=max_query_length,\n",
    "      is_training=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "\n",
    "with open('xlnet-squad-train.pkl', 'wb') as fopen:\n",
    "    pickle.dump([train_features, train_examples], fopen)\n",
    "    \n",
    "with open('xlnet-squad-test.pkl', 'wb') as fopen:\n",
    "    pickle.dump([test_features, test_examples], fopen)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
